class FleetsController < ApplicationController
  # GET /fleets
  # GET /fleets.xml
  def index
    @fleets = Fleet.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @fleets }
    end
  end

  # GET /fleets/1
  # GET /fleets/1.xml
  def show
    @fleet = Fleet.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @fleet }
    end
  end

  # GET /fleets/new
  # GET /fleets/new.xml
  def new
    @fleet = Fleet.new
		@tipservs = ServiceType.find(:all, :order=>'descripcion')

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @fleet }
    end
  end

  # GET /fleets/1/edit
  def edit
    @fleet = Fleet.find(params[:id])
		@buses = Bus.where("fleet_id = ?", @fleet.id)
		@tipservs = ServiceType.find(:all, :order=>'descripcion')
  end

  # POST /fleets
  # POST /fleets.xml
  def create
    @fleet = Fleet.new(params[:fleet])
		@fleet.service_type_id = params[:tipo_servicio]
		@fleet.numBusesDisp = @fleet.numBuses
		@fleet.numBusesNoDisp = 0
    
    if @fleet.save
			for i in 1..@fleet.numBuses
				@bus = Bus.new
				@bus.ultRevisionTecnica = @fleet.fechaAdquisicion
				@bus.estado = 1
				@bus.fleet_id = @fleet.id
				@bus.placa = "SIN PLACA"
				if !@bus.save
					respond_to do |format|
						format.html { render :action => "new" }
						format.xml  { render :xml => @fleet.errors, :status => :unprocessable_entity }
					end
				end
			end		
			respond_to do |format|
		    format.html { redirect_to(@fleet, :notice => 'Fleet was successfully created.') }
		    format.xml  { render :xml => @fleet, :status => :created, :location => @fleet }
			end
    else
			respond_to do |format|
		    format.html { render :action => "new" }
		    format.xml  { render :xml => @fleet.errors, :status => :unprocessable_entity }
		  end
    end
  end

  # PUT /fleets/1
  # PUT /fleets/1.xml
  def update
    @fleet = Fleet.find(params[:id])
		@fleet.service_type_id = params[:tipo_servicio]
		@fleet.numBusesDisp = params[:numBusesDisp]
		@fleet.numBusesNoDisp = params[:numBusesNoDisp]
	
		placas = params[:placas]

    if @fleet.update_attributes(params[:fleet])
			@buses = Bus.where("fleet_id = ?", @fleet.id)

			@buses.each do |b|
				if b.placa == "SIN PLACA" and placas["p#{b.id}"] != "" then
					#Registra la placa ingresada por el usuario
					b.placa = placas["p#{b.id}"]
					#Cambia el estado de bus a HABILITADO
					b.estado = 2
					if !b.save then
						respond_to do |format|
							format.html { render :action => "edit" }
							format.xml  { render :xml => @fleet.errors, :status => :unprocessable_entity }
						end
					end
				end
			end

	    respond_to do |format|
	      format.html { redirect_to(@fleet, :notice => 'Fleet was successfully updated.') }
	      format.xml  { head :ok }
			end
    else
	    respond_to do |format|
		    format.html { render :action => "edit" }
		    format.xml  { render :xml => @fleet.errors, :status => :unprocessable_entity }
	    end
    end
  end

  # DELETE /fleets/1
  # DELETE /fleets/1.xml
  def destroy
    @fleet = Fleet.find(params[:id])
    @fleet.destroy

    respond_to do |format|
      format.html { redirect_to(fleets_url) }
      format.xml  { head :ok }
    end
  end
end
